/**
 * @FileName: DeviceListAdapter.java
 * @Package:com.bdtop.dbl.ui.adapters
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-29
 * @version V1.0
 */

package com.bdtop.dbl.ui.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bdtop.dbl.DBLApplication;
import com.bdtop.dbl.R;
import com.bdtop.dbl.model.Fence;
import com.bdtop.dbl.server.ServerApiFactory;
import com.bdtop.dbl.server.response.BaseResponse;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.NetworkUtils;
import com.bdtop.dbl.utils.Preferences;

/**
 * @ClassName: MsgListAdapter
 * @Description: TODO
 * @author: liyongqiang
 * @date:2014-10-14
 */
public class FenceListAdapter extends ArrayListAdapter<Fence> {

    public FenceListAdapter(Activity context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (null == row) {
            row = LayoutInflater.from(mContext).inflate(R.layout.listitem_fence, null);

            holder = new ViewHolder();
            holder.fenceStatus = (ImageButton) row.findViewById(R.id.imgbtn_status);
            holder.fenceName = (TextView) row.findViewById(R.id.fence_name);
            holder.fenceStatus.setFocusable(false);

            row.setTag(holder);
        }
        else {
            holder = (ViewHolder) row.getTag();
        }

        final Fence fence = getList().get(position);
        holder.fenceName.setText(fence.getFence_name());
        if (fence.getOpen().equals(Constants.FENCE_STATUS_OPEN)) {
            holder.fenceStatus.setImageResource(R.drawable.switch2);
            holder.fenceStatus.setTag(true);
        } else {
            holder.fenceStatus.setImageResource(R.drawable.switch1);
            holder.fenceStatus.setTag(false);
        }

        holder.fenceStatus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Boolean isOpen = (Boolean) view.getTag();
                if (isOpen) {// close fence
                    ((ImageButton) view).setImageResource(R.drawable.switch1);
                } else {// open fence
                    ((ImageButton) view).setImageResource(R.drawable.switch2);
                }
                view.setTag(!isOpen);
                String open;
                if (isOpen) {
                    open = Constants.FENCE_STATUS_CLOSE;
                } else {
                    open = Constants.FENCE_STATUS_OPEN;
                }
//                String mdn =   DBLApplication.getInstance().currentDeviceMdn;
                String mdn = DBLApplication.getInstance().currentDi.mdn;
                ServerApiFactory.getApiImpl().fenceSet(Preferences.getLoginSign(mContext),
//                        Preferences.getCurrentDeviceMdn(mContext),
                        mdn,
                        fence.getFence_id(), open, new RequestCallback() {

                            @Override
                            public void onSuccess(String rawData, Object response) {
                                BaseResponse baseResponse = (BaseResponse) response;
                                if (baseResponse.getStatus().equals(Constants.REQUEST_FAILED)) {
                                    Toast.makeText(mContext, R.string.fence_set_error,
                                            Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(int statusCode, Throwable e, String rawData) {
                                if(NetworkUtils.isNetConnect(mContext)){
                                    Toast.makeText(mContext, R.string.oper_error,Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(mContext, R.string.net_error,Toast.LENGTH_SHORT).show();
                                     }
                            }
                        });
            }
        });
        return row;
    }

    static class ViewHolder {
        ImageButton fenceStatus;
        TextView fenceName;
    }

}
